package com.kristin.thread;

import org.omg.PortableServer.THREAD_POLICY_ID;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @ClassName ThreadMain
 * @Description TODO
 * @Author Kristin
 * @Date 2021/06/24 09:39:47
 * @Version 1.0.0
 */
public class ThreadMain {


    public void joinTest() throws InterruptedException {
        // 主线程等待子线程执行完成场景

        class MyRunnable implements Runnable {

            int i;

            public int getI () {
                return i;
            }

            MyRunnable(int i) {
                this.i = i ;
            }

            @Override
            public void run() {
                i--;
                try {
                    System.out.println("等待计算...");
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        MyRunnable r = new MyRunnable(10);
        Thread t = new Thread(r);
        t.start();
        t.join();
        System.out.println("子线程执行完毕,主线程获取子线程中的计算结果:"+ r.getI());

    }


    public void printABC() {
        class MyRunnable implements Runnable{

            private String o;

            private Object lock;

            private Object unlock;


            public MyRunnable(String o, Object lock, Object unlock) {
                this.o = o;
                this.lock = lock;
                this.unlock = unlock;
            }

            @Override
            public void run() {
                while(true) {
                    synchronized (lock) {
                        // 获取到锁，打印
                        System.out.println(o);
                        // 打印完成，唤醒
                        // 打印
                    }
                    // 唤醒打印C线程
                }
            }
        }

    }

   static  class MyThreadFactory implements ThreadFactory {

        private final static AtomicInteger threadNum = new AtomicInteger(1);

       private final static ThreadGroup threadGroup = new ThreadGroup("kristin-thread-group");

       @Override
        public Thread newThread(Runnable r) {
           Thread thread = new Thread(threadGroup, r, "kristin-thread-" + threadNum.getAndIncrement());

            return thread;
        }
    }


    static class MyRunnable implements Runnable{
        @Override
        public void run() {
            System.out.println(String.format("[%s]runnable执行", Thread.currentThread().getName()));
        }
    }

    static class MyCallable implements Callable{

        private static final AtomicInteger num = new AtomicInteger(1);

        @Override
        public Object call() throws Exception {
            System.out.println(String.format("[%s]callable执行", Thread.currentThread().getName()));
            return "执行结果" + num.getAndIncrement();
        }
    }


    public static void main(String[] args) throws InterruptedException {
//
////        new ThreadMain().joinTest();
//        new ThreadMain().printABC();
//        ThreadLocal threadlocal = new ThreadLocal();
//        threadlocal.set("123");





        // 线程池
//        ThreadPoolExecutor tpl = new ThreadPoolExecutor(5, 5, 60, TimeUnit.SECONDS, new ArrayBlockingQueue(100),new MyThreadFactory());
//        for(int i = 0 ; i < 10; i++) {
//            tpl.submit(new MyRunnable());
//        }
//        for(int i = 0 ; i < 10; i++) {
//            tpl.submit(new MyCallable());
//        }

        //Callable测试
//        FutureTask<String> task = new FutureTask<String>(new MyCallable());
//        new Thread(task).start();
//
//        try {
//            while(true) {
//                if(task.isDone()) {
//                    System.out.println("执行结果" + task.get());
//                    break;
//                }
//                System.out.println("等待结果");
//            }
//        } catch (ExecutionException e) {
//            e.printStackTrace();
//        }

    }


}
